package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import bean.DBHelper;
import bean.Achievement;
import bean.Student;

public class AchievementDao {
	public void addAchievement(Achievement a){
		String sql="insert into achievement (sno,eid,score) values('"+a.getSno()+"','"+a.getEid()+"','"+a.getScore()+"')";
		DBHelper.executeUpdate(sql);
		DBHelper.close();
	}
	public ArrayList<Achievement> allAchievement() {
		ArrayList<Achievement> list = new ArrayList<Achievement>();
		String sql = "select sno,eid,score from achievement";
		ResultSet rs = DBHelper.executeQuery(sql);
        try{
		while (rs.next()) {
			Achievement ach = new Achievement();
				ach.setSno(rs.getString(1));
				ach.setEid(rs.getInt(2));
				ach.setScore(rs.getInt(3));			
				list.add(ach);
			}
        } 
        catch (Exception ex) 
        {
			ex.printStackTrace();
		}
        DBHelper.close();
		return list;
	}
	public ArrayList<Achievement> allAchievementInSql(String sql) {
		ArrayList<Achievement> list = new ArrayList<Achievement>();
		String SQL = sql;
		ResultSet rs = DBHelper.executeQuery(SQL);
        try{
		while (rs.next()) {
			Achievement ach = new Achievement();
				ach.setSno(rs.getString(1));
				ach.setEid(rs.getInt(2));
				ach.setScore(rs.getInt(3));			
				list.add(ach);
			}
        } 
        catch (Exception ex) 
        {
			ex.printStackTrace();
		}
        DBHelper.close();
		return list;
	}
	public ArrayList<Achievement> getOnePage(int position,int pageSize,String sql) {
		ArrayList<Achievement> list = new ArrayList<Achievement>();
		String SQL = sql;
		ResultSet rs = DBHelper.executeQuery(SQL);
		 try{
			 
				for(int i=0;i<position;i++){
					rs.next();
				}
				for(int j=0;j<pageSize;j++){
					Achievement ach = new Achievement();
					ach.setSno(rs.getString(1));
					ach.setEid(rs.getInt(2));
					ach.setScore(rs.getInt(3));			
					list.add(ach);
					rs.next();
				}
		        } 
		        catch (Exception ex) 
		        {
					ex.printStackTrace();
				}
		        DBHelper.close();
				return list;
	}
	public ArrayList<Achievement> getLastPage(int position,String sql) {
		ArrayList<Achievement> list = new ArrayList<Achievement>();
		String SQL =sql;
		ResultSet rs = DBHelper.executeQuery(SQL);
		 try{
			 
				for(int i=0;i<position;i++){
					rs.next();
				}
				do{
					Achievement ach = new Achievement();
						ach.setSno(rs.getString(1));
						ach.setEid(rs.getInt(2));
						ach.setScore(rs.getInt(3));			
						list.add(ach);
					}while (rs.next()) ;
		        } 
		        catch (Exception ex) 
		        {
					ex.printStackTrace();
				}
		        DBHelper.close();
				return list;
	}
}
